﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPMonitor.Implementation;
using NPMonitor.DataModel.Report;
using NPMonitor.DataModel;
using System.Web.UI.DataVisualization.Charting;

namespace NPMonitor.Web.Reports
{
    public partial class CumulativeTimeAvg : BasePage
    {
        public override string FunctionName
        {
            get { return FunctionEnum.TimeAverage.ToString(); }
        }


        public string ReportType
        {
            get
            {
                var type = Request.QueryString["ReportType"];
                if (String.IsNullOrWhiteSpace(type))
                {
                    return "Daily";
                }
                else
                {
                    return type;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControl();

                InitPage();
            }
        }

        protected void ddlCase_SelectedIndexChanged(object sender, EventArgs e)
        {
            int? caseId = ddlCase.SelectedValue.ToInteger();

            if (caseId != null)
            {
                BindScenario(caseId.Value);
            }
        }

        protected void InitPage()
        {
            rtpTimeSelector.EndDate = DateTime.UtcNow.ToDateHourString();
            rtpTimeSelector.BeginDate = DateTime.UtcNow.GetBeginTimeOfDailyReport().ToDateHourString();

            rfvCase.ErrorMessage = string.Format(ValidationString.PleaseSelect, "Case");
            rfvLocation.ErrorMessage = string.Format(ValidationString.PleaseSelect, "Location");
        }

        protected void btnViewReport_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void BindControl()
        {
            BindLocationName();

            BindCase();
        }

        protected void BindLocationName()
        {
            var locationList = StoreManager.Current.MachineStore.GetAllMachinePool();
            ddlLocation.BindDataSource(locationList, "PoolName", "PoolId");
            ddlLocation.SelectedIndex = 0;
        }

        protected void BindCase()
        {
            var caseList = StoreManager.Current.JobDefinitionStore.GetAllJobDefinitions(0, -1);
            ddlCase.BindDataSource(caseList, "JobName", "JobId");
        }

        protected void BindScenario(int caseId)
        {
            IList<Scenario> scenarioList = new List<Scenario>();
            scenarioList = StoreManager.Current.JobDefinitionStore.GetEnableScenarios(caseId);

            chkScenario.DataSource = scenarioList;
            chkScenario.DataTextField = "ScenarioName";
            chkScenario.DataValueField = "ScenarioNo";
            chkScenario.DataBind();

            phScenario.Visible = !(scenarioList.Count == 0);

            for (int i = 0; i < chkScenario.Items.Count; i++)
            {
                chkScenario.Items[i].Selected = true;
            }
        }

        private void BindData()
        {
            var beginTime = rtpTimeSelector.BeginDate;
            var endTime = rtpTimeSelector.EndDate;
            string locationId = ddlLocation.SelectedValue;
            var reportTime = string.Format("{0} - {1}", beginTime.ToReportDatetimeFormat(), endTime.ToReportDatetimeFormat());
            var jobName = ddlCase.SelectedItem.Text;
            var reportTitle = string.Format("{0}({1})", "DSM", jobName);
            if (beginTime != "" && endTime != "")
            {
                int? caseId = hfCaseName.Value.ToInteger();

                List<ReportData> datalist;
                List<string> scenarioNoList = new List<string>();

                for (int i = 0; i < chkScenario.Items.Count; i++)
                {
                    if (chkScenario.Items[i].Selected)
                    {
                        scenarioNoList.Add(chkScenario.Items[i].Value);
                    }
                }

                //ucReport.ChartType = SeriesChartType.StackedArea;

                switch (ReportType)
                {
                    case "Daily":
                        datalist = StoreManager.Current.ReportStore.GetCumulativeTimeAvgReportData(locationId.ToInteger().Value, beginTime, endTime, scenarioNoList.ToArray());
                        ucReport.BindReportData(datalist, null, @"Reports\rdlc\ReportCumulativeTimeAvg.rdlc", reportTime, reportTitle);
                        break;
                    default:
                        datalist = new List<ReportData>();
                        break;
                }
            }
        }
    }
}